<?php namespace App\Models;

use Watson\Validating\ValidatingTrait;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Class Author
 *
 * @package App\Models
 * @property int $id
 * @property int $user_id 用户id
 * @property int $entry_form_id 报名表id
 * @property string $name 姓名
 * @property string $id_card 身份证号
 * @property string $mobile 手机号码
 * @property string $qq QQ
 * @property string $created_at
 * @property string $updated_at
 * @property string $sign_pic 手写签名图片
 * @property-read \App\Models\EntryForm $entryForm
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Works[] $works
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Author whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Author whereUserId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Author whereEntryFormId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Author whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Author whereIdCard($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Author whereMobile($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Author whereQq($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Author whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Author whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Author whereSignPic($value)
 * @mixin \Eloquent
 */
class Author extends Model
{
    use ValidatingTrait;

    protected $fillable = ['name', 'id_card', 'mobile', 'qq'];

    protected $rules = [
        'user_id'       => 'required|integer',
        'entry_form_id' => 'required|integer',
        'name'          => 'required',
        'id_card'       => 'required',
        'mobile'        => 'required',
    ];

    protected $validationMessages = [
        'user_id.required'       => '用户id必填',
        'user_id.integer'        => '用户id格式有误',
        'entry_form_id.required' => '报名表id必填',
        'entry_form_id.integer'  => '报名表格式有误',
        'name.required'          => '姓名必填',
        'id_card.required'       => '身份证号必填',
        'mobile.required'        => '手机号码必填',
    ];

    /**
     * 所属报名表
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function entryForm()
    {
        return $this->belongsTo('\App\Models\EntryForm', 'entry_form_id');
    }

    /**
     * 该作者参与的作品
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function works()
    {
        return $this->belongsToMany('\App\Models\Works', 'works_author', 'author_id', 'works_id');
    }

    /**
     * 上传手写签名
     * @param UploadedFile $file
     */
    public function uploadSignPicFile(UploadedFile $file)
    {
        $uploadFileTypes = ['image/png', 'image/jpg', 'image/jpeg',];
        $uploadFileExtensions = ['png', 'jpg', 'jpeg'];
        //校验文件类型
        if (!in_array($file->getMimeType(), $uploadFileTypes) ||
            !in_array(strtolower($file->getClientOriginalExtension()), $uploadFileExtensions)
        ) {
            throw new FileException('文件类型有误,只允许上传png,jpg,jpeg');
        }

        $this->sign_pic = $this->id . '.' . strtolower($file->getClientOriginalExtension());
        if (!\Storage::disk('sign_file')->put($this->sign_pic, \File::get($file))) {
            throw new FileException('保存文件失败.');
        } else {
            $this->save();
        }
    }

    /**
     * 获取上传手写签名url
     * @return string
     */
    public function getSignPicUrl()
    {
        if ($this->sign_pic) {
            return config('filesystems.disks.sign_file.url_prefix') . $this->sign_pic;
        } else {
            return '';
        }
    }
}